<?php
/**
 * Created by PhpStorm.
 * User: tt
 * Date: 2017/12/27
 * Time: 14:31
 */

namespace app\admin\logic;
use app\admin\model\Store as Store_model;
use think\Cache;
use nx\Data;


class Store
{
    private $Store_model = '';

    public function __construct(){
        $this->Store_model = new Store_model();
    }

    /**
     * 获取所有的门店（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function storesToText(){
        $states = self::getAllKindergartens();
        $states = collect($states)->column('name','id');
        return $states;
    }

    /**
     * 获得所有的门店（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllStores(){
        if (!empty(Cache::get('allStores'))){
            return Cache::get('allStores');
        }else{
            $states = Store_model::all();
            Cache::set('allStores',$states);
            return $states;
        }
    }

    /**
     * 获取启用的门店
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getEnabledStores(){
        $states = $this->getAllStores();
        $states = collect($states)->where('status',1)->all();
        $states = collection($states)->toArray();
        $states = array_merge($states);
        return $states;
    }

    public function addStore($data){
        if(empty($data)){
            return ['result'=>2,'msg'=>'数据丢失'];
        }
        $data['status'] = 1;//自动启用
        $result = $this->Store_model->allowField(true)->validate(true)->save($data);
        if($result == false){
            return ['result'=>2,'msg'=>$this->Store_model->getError()];
        }else{
            Cache::rm('allStores');
            return ['result'=>1,'msg'=>'新增成功'];
        }
    }

    public function editStore($data){
        if(empty($data)){
            return ['result'=>2,'msg'=>'数据丢失'];
        }
        if(empty($data['id'])){
            return ['result'=>2,'msg'=>'ID丢失'];
        }
        $result = $this->Store_model->allowField(true)->validate('Store.edit')->isUpdate(true)->save($data);
        if($result == false){
            return ['result'=>2,'msg'=>$this->Store_model->getError()?:'没有需要修改的数据'];
        }else{
            Cache::rm('allStores');
            return ['result'=>1,'msg'=>'修改成功'];
        }
    }

    public function delStore($id)
    {
        if (empty($id)) {
            return ['result' => false, 'msg' => '请选择要操作的数据'];
        }
        if (Store_model::get($id)) {
            if (Store_model::destroy($id)) {
                Cache::rm('allStores');
                return ['result' => true, 'msg' => '删除成功'];
            } else {
                return ['result' => false, 'msg' => '删除失败'];
            }
        } else {
            return ['result' => false, 'msg' => '要删除的数据不存在'];
        }
    }

    /**将所有类别进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAllStoreToTree()
    {
        $Classifys = $this->getAllStores();
        $Classifys = Data::tree($Classifys, 'title', 'id', 'pid');
        return $Classifys;
    }

}